home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-rdflib / rdflib / util.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  6.9 KB  |  241 lines

  1. from rdflib.URIRef import URIRef
  2. from rdflib.BNode import BNode
  3. from rdflib.Literal import Literal
  4. from rdflib.Variable import Variable
  5. from rdflib.Graph import Graph, QuotedGraph
  6. from rdflib.Statement import Statement
  7.  
  8. from rdflib.exceptions import SubjectTypeError, PredicateTypeError, ObjectTypeError, ContextTypeError
  9. from rdflib.compat import rsplit
  10. from cPickle import loads
  11.  
  12. def list2set(seq):
  13.     seen = set()
  14.     return [ x for x in seq if x not in seen and not seen.add(x)]
  15.  
  16. def first(seq):
  17.     for result in seq:
  18.         return result
  19.     return None
  20.  
  21. def uniq(sequence, strip=0):
  22.     """removes duplicate strings from the sequence."""
  23.     set = {}
  24.     if strip:
  25.         map(lambda val, default: set.__setitem__(val.strip(), default),
  26.             sequence, [])
  27.     else:
  28.         map(set.__setitem__, sequence, [])
  29.     return set.keys()
  30.  
  31. def more_than(sequence, number):
  32.     "Returns 1 if sequence has more items than number and 0 if not."
  33.     i = 0
  34.     for item in sequence:
  35.         i += 1
  36.         if i > number:
  37.             return 1
  38.     return 0
  39.  
  40. def term(str, default=None):
  41.     """See also from_n3"""
  42.     if not str:
  43.         return default
  44.     elif str.startswith("<") and str.endswith(">"):
  45.         return URIRef(str[1:-1])
  46.     elif str.startswith('"') and str.endswith('"'):
  47.         return Literal(str[1:-1])
  48.     elif str.startswith("_"):
  49.         return BNode(str)
  50.     else:
  51.         msg = "Unknown Term Syntax: '%s'" % str
  52.         raise Exception(msg)
  53.  
  54.  
  55.  
  56. from time import mktime, time, gmtime, localtime, timezone, altzone, daylight
  57.  
  58. def date_time(t=None, local_time_zone=False):
  59.     """http://www.w3.org/TR/NOTE-datetime ex: 1997-07-16T19:20:30Z
  60.  
  61.     >>> date_time(1126482850)
  62.     '2005-09-11T23:54:10Z'
  63.  
  64.     >>> date_time(1126482850, local_time_zone=True)
  65.     '2005-09-11T19:54:10-04:00'
  66.  
  67.     >>> date_time(1)
  68.     '1970-01-01T00:00:01Z'
  69.  
  70.     >>> date_time(0)
  71.     '1970-01-01T00:00:00Z'
  72.     """
  73.     if t is None:
  74.         t = time()
  75.  
  76.     if local_time_zone:
  77.         time_tuple = localtime(t)
  78.         if time_tuple[8]:
  79.             tz_mins = altzone // 60
  80.         else:
  81.             tz_mins = timezone // 60
  82.         tzd = "-%02d:%02d" % (tz_mins // 60, tz_mins % 60)
  83.     else:
  84.         time_tuple = gmtime(t)
  85.         tzd = "Z"
  86.  
  87.     year, month, day, hh, mm, ss, wd, y, z = time_tuple
  88.     s = "%0004d-%02d-%02dT%02d:%02d:%02d%s" % ( year, month, day, hh, mm, ss, tzd)
  89.     return s
  90.  
  91. def parse_date_time(val):
  92.     """always returns seconds in UTC
  93.  
  94.     # tests are written like this to make any errors easier to understand
  95.     >>> parse_date_time('2005-09-11T23:54:10Z') - 1126482850.0
  96.     0.0
  97.  
  98.     >>> parse_date_time('2005-09-11T16:54:10-07:00') - 1126482850.0
  99.     0.0
  100.  
  101.     >>> parse_date_time('1970-01-01T00:00:01Z') - 1.0
  102.     0.0
  103.  
  104.     >>> parse_date_time('1970-01-01T00:00:00Z') - 0.0
  105.     0.0
  106.     >>> parse_date_time("2005-09-05T10:42:00") - 1125916920.0
  107.     0.0
  108.     """
  109.  
  110.     if "T" not in val:
  111.         val += "T00:00:00Z"
  112.  
  113.     ymd, time = val.split("T")
  114.     hms, tz_str = time[0:8], time[8:]
  115.  
  116.     if not tz_str or tz_str=="Z":
  117.         time = time[:-1]
  118.         tz_offset = 0
  119.     else:
  120.         signed_hrs = int(tz_str[:3])
  121.         mins = int(tz_str[4:6])
  122.         secs = (cmp(signed_hrs, 0) * mins + signed_hrs * 60) * 60
  123.         tz_offset = -secs
  124.  
  125.     year, month, day = ymd.split("-")
  126.     hour, minute, second = hms.split(":")
  127.  
  128.     t = mktime((int(year), int(month), int(day), int(hour),
  129.                 int(minute), int(second), 0, 0, 0))
  130.     t = t - timezone + tz_offset
  131.     return t
  132.  
  133. def from_n3(s, default=None, backend=None):
  134.     """ Creates the Identifier corresponding to the given n3 string. WARNING: untested, may contain bugs. TODO: add test cases."""
  135.     if not s:
  136.         return default
  137.     if s.startswith('<'):
  138.         return URIRef(s[1:-1])
  139.     elif s.startswith('"'):
  140.         # TODO: would a regex be faster?
  141.         value, rest = rsplit(s, '"', 1)
  142.         value = value[1:] # strip leading quote
  143.         if rest.startswith("@"):
  144.             if "^^" in rest:
  145.                 language, rest = rsplit(rest, '^^', 1)
  146.                 language = language[1:] # strip leading at sign
  147.             else:
  148.                 language = rest[1:] # strip leading at sign
  149.                 rest = ''
  150.         else:
  151.             language = None
  152.         if rest.startswith("^^"):
  153.             datatype = rest[3:-1]
  154.         else:
  155.             datatype = None
  156.         value = value.replace('\\"', '"').replace('\\\\', '\\').decode("unicode-escape")
  157.         return Literal(value, language, datatype)
  158.     elif s.startswith('{'):
  159.         identifier = from_n3(s[1:-1])
  160.         return QuotedGraph(backend, identifier)
  161.     elif s.startswith('['):
  162.         identifier = from_n3(s[1:-1])
  163.         return Graph(backend, identifier)
  164.     else:
  165.         if s.startswith("_:"):
  166.             return BNode(s[2:])
  167.         else:
  168.             return BNode(s)
  169.  
  170. def check_context(c):
  171.     if not (isinstance(c, URIRef) or \
  172.             isinstance(c, BNode)):
  173.         raise ContextTypeError("%s:%s" % (c, type(c)))
  174.  
  175. def check_subject(s):
  176.     """ Test that s is a valid subject identifier."""
  177.     if not (isinstance(s, URIRef) or isinstance(s, BNode)):
  178.         raise SubjectTypeError(s)
  179.  
  180. def check_predicate(p):
  181.     """ Test that p is a valid predicate identifier."""
  182.     if not isinstance(p, URIRef):
  183.         raise PredicateTypeError(p)
  184.  
  185. def check_object(o):
  186.     """ Test that o is a valid object identifier."""
  187.     if not (isinstance(o, URIRef) or \
  188.             isinstance(o, Literal) or \
  189.             isinstance(o, BNode)):
  190.         raise ObjectTypeError(o)
  191.  
  192. def check_statement((s, p, o)):
  193.     if not (isinstance(s, URIRef) or isinstance(s, BNode)):
  194.         raise SubjectTypeError(s)
  195.  
  196.     if not isinstance(p, URIRef):
  197.         raise PredicateTypeError(p)
  198.  
  199.     if not (isinstance(o, URIRef) or \
  200.             isinstance(o, Literal) or \
  201.             isinstance(o, BNode)):
  202.         raise ObjectTypeError(o)
  203.  
  204. def check_pattern((s, p, o)):
  205.     if s and not (isinstance(s, URIRef) or isinstance(s, BNode)):
  206.         raise SubjectTypeError(s)
  207.  
  208.     if p and not isinstance(p, URIRef):
  209.         raise PredicateTypeError(p)
  210.  
  211.     if o and not (isinstance(o, URIRef) or \
  212.                   isinstance(o, Literal) or \
  213.                   isinstance(o, BNode)):
  214.         raise ObjectTypeError(o)
  215.  
  216. def graph_to_dot(graph, dot):
  217.     """ Turns graph into dot (graphviz graph drawing format) using pydot. """
  218.     import pydot
  219.     nodes = {}
  220.     for s, o in graph.subject_objects():
  221.         for i in s,o:
  222.             if i not in nodes.keys():
  223.                 nodes[i] = i
  224.     for s, p, o in graph.triples((None,None,None)):
  225.         dot.add_edge(pydot.Edge(nodes[s], nodes[o], label=p))
  226.  
  227.  
  228. if __name__ == "__main__":
  229.     # try to make the tests work outside of the time zone they were written in
  230.     #import os, time
  231.     #os.environ['TZ'] = 'US/Pacific'
  232.     #try:
  233.     #    time.tzset()
  234.     #except AttributeError, e:
  235.     #    print e
  236.         #pass
  237.         # tzset missing! see
  238.         # http://mail.python.org/pipermail/python-dev/2003-April/034480.html
  239.     import doctest
  240.     doctest.testmod()
  241.